package com.fs.service.impl;


import com.fs.dao.ClassDao;
import com.fs.dao.ClassStudentDao;
import com.fs.dao.CourseDao;
import com.fs.dao.UserDao;
import com.fs.pojo.Class;
import com.fs.pojo.ClassStudent;
import com.fs.pojo.Course;
import com.fs.pojo.User;
import com.fs.service.CourseService;
import com.fs.utils.BeanFactories;
import com.fs.utils.IdWorker;
import com.fs.vo.CourseClassVO;
import com.fs.vo.CourseVO;
import net.sf.cglib.beans.BeanCopier;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (TbCourse)表服务实现类
 *
 * @author makejava
 * @since 2022-11-15 10:21:17
 */

public class CourseServiceImpl implements CourseService {

    private CourseDao courseDao = BeanFactories.getBean(CourseDao.class);
    private UserDao userDao  = BeanFactories.getBean(UserDao.class);
    private ClassStudentDao classStudentDao = BeanFactories.getBean(ClassStudentDao.class);
    private ClassDao classDao = BeanFactories.getBean(ClassDao.class);


    @Override
    public List<CourseClassVO> queryCourseClassList(String teacherId) {

        // 通过教师ID获取获取所有课程

        List<Course> courses = courseDao.queryCoursesByTeacherId(teacherId);

        List<CourseClassVO> classVOS = courses.stream().map(course -> {

            CourseClassVO vo = new CourseClassVO();

            //查询此课程对应的所有班级
            List<Class> classes = classDao.queryClassesByCourseId(course.getId());

            vo.setClassList(classes);

            BeanCopier.create(Course.class, CourseClassVO.class, false).copy(course, vo, null);

            return vo;
        }).collect(Collectors.toList());


        return classVOS;
    }

    @Override
    public boolean delCourse(String courseId) {

        return  courseDao.deleteById(courseId)>0 ? true : false;

    }

    @Override
    public boolean updateCourse(Course course) {
        return  courseDao.update(course)>0 ? true : false;
    }

    @Override
    public Course createCourse(Course course) {
        String id = courseDao.insertReturnId(course);
        if(id!=null){
           return courseDao.queryById(id);
        }
        return null;
    }

    @Override//通过学生id查询课程信息
    public List<CourseVO> queryAllCourseByStudentId(String id) {

        //先通过学生id查询所有他在的班级id
        List<ClassStudent> classStudents = classStudentDao.QueryAllByStudentId(id);

        List<CourseVO> vos = classStudents.stream().map(classStudent -> {

            CourseVO vo = new CourseVO();

            //在通过班级id查询课程id
            Class myClass = classDao.queryById(classStudent.getClassId());

            //课程id查询真正课程信息
            Course course = courseDao.queryById(myClass.getCourseId());

            //拿到教师id后差查询 装入VO
            User user = userDao.queryById(course.getTeacherId());

            BeanCopier.create(User.class, CourseVO.class, false).copy(user, vo, null);
            BeanCopier.create(ClassStudent.class, CourseVO.class, false).copy(classStudent, vo, null);
            BeanCopier.create(Class.class, CourseVO.class, false).copy(myClass, vo, null);
            BeanCopier.create(Course.class, CourseVO.class, false).copy(course, vo, null);

            return vo;

        }).collect(Collectors.toList());


        return vos;
    }

    @Override//通过教师id查询课程信息
    public List<CourseVO> queryAllCourseByTeacherId(String id) {

        //先通过教师id在course表查询所有所教课程
        List<Course> courses = courseDao.queryCoursesByTeacherId(id);

        List<CourseVO> vos = courses.stream().map(course -> {

            CourseVO vo = new CourseVO();

            //在通过课程id查询班级id
            Course myCourse = courseDao.queryById(course.getId());

            //课程id查询真正课程信息
            Class myClass = classDao.queryById(myCourse.getId());

            //装入VO
            BeanCopier.create(Class.class, CourseVO.class, false).copy(myClass, vo, null);
            BeanCopier.create(Course.class, CourseVO.class, false).copy(course, vo, null);

            return vo;

        }).collect(Collectors.toList());


        return vos;
    }

    @Override//通过学生ID和课程暗号添加课程信息
    public int addClassStudentByCourseCode(String code,String id) {

        //先通过课程暗号查询课程id
        Class myclass = classDao.QueryAllByCourseCode(code);

        IdWorker claStuId =  new IdWorker();
        //通过课程ID和用户登录ID添加数据到class_student表中
        ClassStudent classStudent = new ClassStudent();
        classStudent.setId(""+claStuId.nextId());
        classStudent.setClassId(myclass.getId());
        classStudent.setStudentId(id);
        classStudent.setIsTop(1);

        return classStudentDao.insert(classStudent);
    }

    @Override//根据课程名称删除课程学生记录
    public void deleteClassStudentByCourseName(List<String> id,String studentId) {
        List<Class> classList = new ArrayList<>();
        for (String courseId : id) {
            //通过课程id查询班级id
            Class myClass = classDao.QueryAllByCourseId(courseId);
            classList.add(myClass);
        }
        for (Class aClass : classList) {
            //通过班级ID和学生id删除class_student表中数据
            classStudentDao.deleteByClassIdAndStudenID(aClass.getId(),studentId);
        }
    }

    @Override//通过课程暗号查询所有课程（返回值  1有   0无）
    public int queryByCourseCode(String code) {
        Class aClass = classDao.QueryAllByCourseCode(code);
        if(aClass != null){
            //有返回值，说明有这个课程暗号
            return 1;
        }
        return 0;
    }

    @Override
    public List<Course> queryAllCourse() {
        return null;
    }


}
